//
//  HNCourseListVC.m
//  优学荟管理系统
//
//  Created by 星道 on 2017/10/7.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "HNCourseListVC.h"
#import "CourseEvaluationSegmentVC.h"
#import "AddCourseVC.h"
#import "HNCourseListVM.h"
#import "CurriculumSignedVC.h"
#import "CurriculumAllEvaVC.h"
#import "ZYAddStrongCouseController.h"
#import "HNCourseListSeeTeacherVC.h"
#import "HNCourseListSeeTeacherSearchView.h"
#import "LLNavigationMidSearchItem.h"
#import "HNStudentSignVC.h"
#import "HNThreeGoodEvaVC.h"
#import "HNHandleCourseVC.h"

@interface HNCourseListVC ()<HNCourseListDelegate,UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITableView *TableView;
@property (strong, nonatomic) HNCourseListVM *VM;
@property (nonatomic,strong) LLNavigationMidSearchItem *midItem;
@property(nonatomic, strong) UITextField *searchTextField;
@end

@implementation HNCourseListVC


#pragma mark - cycle life
- (void)viewDidLoad {

    [super viewDidLoad];
    self.title = @"课程列表";
    [self.VM ConfigTableView:self.TableView];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加课程" style:(UIBarButtonItemStyleDone) target:self action:@selector(GoAddCourse)];
    // 搜索
    UIView *vv = [[UIView alloc]initWithFrame:CGRectMake(50, 0, kScreenWidth - 100, 30)];
    vv.backgroundColor = [UIColor redColor];
    self.navigationItem.titleView = vv;
    
    self.midItem = [LLNavigationMidSearchItem new];
    self.midItem.frame = CGRectMake(0, 0, kScreenWidth - 120, 30);
    self.midItem.searchField.placeholder = @"搜索课程或老师";
    self.navigationItem.titleView = self.midItem;
    
    self.searchTextField = self.midItem.searchField;
    self.searchTextField.delegate = self;
    self.searchTextField.returnKeyType =  UIReturnKeySearch;
    
    
//    self.navigationItem.titleView = [HNCourseListSeeTeacherSearchView ConfigSeeTeacherSearchViewWithComplete:^(NSString *Str) {
//        [self.VM Search:Str];
//    }];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.searchTextField resignFirstResponder];
    
    NSString *strUrl = [self.searchTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    [self.VM Search:strUrl];
    
    
    return YES;
    
}





#pragma mark - cell delegate
/** 添加课程 */
- (void)GoAddCourse {

    [self.VM GoAddCourse];
}

/** 签到 */
- (void)Cell:(HNCourseListCell *)Cell QianDaoWithModel:(CourseListModel *)Model {

//    CurriculumSignedVC *VC = [[CurriculumSignedVC alloc] init];
//    VC.ID = Model.ID;
//    VC.IsTeacher = NO;
//    [self.navigationController pushViewController:VC animated:YES];
    HNStudentSignVC *VC = [[HNStudentSignVC alloc] init];
    VC.ID = Model.ID;
    [self.navigationController pushViewController:VC animated:YES];
}

/** 编辑 */
- (void)Cell:(HNCourseListCell *)Cell EditWithModel:(CourseListModel *)Model {

    HNHandleCourseVC *VC = [[HNHandleCourseVC alloc] init];
    // 声明我这是修改课程
    VC.type = HNHandleCourseTypeEdit;
    // 对应的课程
    VC.Id = Model.ID;
    VC.PopOfRefresh = ^{
        
        [self.TableView.mj_header beginRefreshing];
    };
    [self.navigationController pushViewController:VC animated:YES];
    return;
    
    if (10 == [Model.Typexxxx integerValue]) {
          [self ClickCreatCourse:[Model.ID stringValue] yaosuModel:Model.ElementCountArr];
    }else{
        AddCourseVC *VC = [[AddCourseVC alloc] init];
        // 声明我这是修改课程
        VC.mode = AddCourseUpload;
        // 对应的课程
        VC.lessionID = Model.ID;
        VC.backPop = ^{
            
            
            [_TableView.mj_header beginRefreshing];
        };
        
        [self.navigationController pushViewController:VC animated:YES];
        
    }
    
}

/** 查看老师 */
- (void)Cell:(HNCourseListCell *)Cell SeeTeacherWithModel:(CourseListModel *)Model {
    
    HNCourseListSeeTeacherVC *VC = [[HNCourseListSeeTeacherVC alloc] init];
    VC.lessonId = Model.ID;
    [self.navigationController pushViewController:VC animated:YES];
}



- (void)ClickCreatCourse:(NSString *)lessonId yaosuModel:(NSMutableArray *)arr {
    
    
    

            
    
    ZYAddStrongCouseController *VC = [[ZYAddStrongCouseController alloc] init];
            // 声明我这是增加课程
    
            
    
    VC.dataSelectSource = arr;
    
    VC.lessionId = lessonId;
    VC.isUpTeach = YES;
    VC.isUpTow = YES;
    VC.isCity = YES;
    VC.titleName = @"编辑加强课程";
    
    VC.popBlock = ^{
                
    
        [self.VM RefreshTableView];
        
    };
            
            
    
    [self.navigationController pushViewController:VC animated:YES];
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}



















/** 三好评论 */
- (void)Cell:(HNCourseListCell *)Cell CommentWithModel:(CourseListModel *)Model {
    
    HNThreeGoodEvaVC *VC = [[HNThreeGoodEvaVC alloc] init];    VC.ID = Model.ID;
    VC.Name = Model.name;
    VC.HNThreeGoodEvaMode = HNThreeGoodEvaModeCourseList;
    [self.navigationController pushViewController:VC animated:YES];
    
}

/** 上下架 */
- (void)Cell:(HNCourseListCell *)Cell ShangXiaJiaWithModel:(CourseListModel *)Model {//0上 1 x

    NSString *url = [NSString stringWithFormat:@"lesson.do?action=changeSellStatus&id=%@&sellStatus=%@",Model.ID,[Model.sellStatus integerValue] == 0 ? @"1":@"0"];
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        [HUD showSuccess:Obj[@"msg"]];
        if ([Obj[@"code"] isEqualToNumber:@1000]) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.VM RefreshTableView];
            });
        }
    } Fail:^(NSError *Error) {
        [HUD showError:NetErr];
    }];

}

#pragma mark - get
- (HNCourseListVM *)VM {

    if (!_VM) {

        _VM = [[HNCourseListVM alloc] init];
        _VM.VC = self;
    }
    return _VM;
}


- (void)RefreshDataWithTeacher:(void (^)(NSArray <TeacherCourseSelectModel *>*Arr))Complete {
    
    [MBProgressHUD showHUD];
    NSDictionary *Params = @{
                             @"action":@"getTeacherByOid",
                             @"oid":[AccountTool shareAccountTool].currentAccount.oid
                             };
    [NetManager Get:LLBaseURL(@"lesson.do") Auth:YES Params:Params Success:^(id Obj) {
        
        if ([Obj[@"code"] isEqualToNumber:@1000]) {
            
            NSMutableArray *MutableArr = [NSMutableArray array];
            [Obj[@"t"] enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                [MutableArr addObject:[TeacherCourseSelectModel HNObjectValuesForKey:obj]];
            }];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                Complete ? Complete ([MutableArr copy]) : nil ;
            });
            
            
        } else {
            [MBProgressHUD showError:Obj[@"msg"]];
        }
    } Fail:^(NSError *Error) {
        [MBProgressHUD showError:NetErr];
    }];
}






@end
